package com.tek.ding.faceding.network.rx.observable;

import android.content.Context;
import android.util.Log;

import com.tek.ding.faceding.base.BaseView;
import com.tek.ding.faceding.network.exception.ApiException;
import com.tek.ding.faceding.network.rx.ProgressDialogHandle;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * 加载进度条
 */
public abstract class ProgressBarObservable<T> extends ErrorHandleObservable<T> implements ProgressDialogHandle.OnProgressDialogListener {


    private BaseView baseView;
    private Disposable disposable;

    public ProgressBarObservable(Context context, BaseView baseView) {
        super(context);
        this.baseView = baseView;
    }

    protected boolean isShowDialog() {
        return true;
    }

    @Override
    public void onCancelProgress() {
        //isDisposed():查询是否解除订阅 true 代表 已经解除订阅
        if (disposable != null && !disposable.isDisposed()) {
            //主动解除订阅
            Log.d("onCancelProgress", "取消订阅");
            disposable.dispose();
        }
    }


    @Override
    public void onSubscribe(@NonNull Disposable d) {
        disposable = d;
        //显示加载
        if (isShowDialog()) {
            baseView.showLoading();
        }
    }

    @Override
    public void onComplete() {
        //显示内容
        baseView.dismissLoading();
    }

    @Override
    public void onError(@NonNull Throwable e) {
//        super.onError(e);
        baseView.dismissLoading();
        //显示提示消息
//        BaseException baseException = rxErrorHandle.handleError(e);
        String message = ((ApiException) e).getMessage();
        baseView.showError(message);
    }
}
